//
//  UIView+DDExtetion.m
//  ddhw_ios
//
//  Created by sv on 16/12/15.
//  Copyright © 2016年 DDTM. All rights reserved.
//

#import "UIView+DDExtetion.h"

@implementation UIView (DDExtetion)

- (void)viewWithcornerRadius:(CGFloat)cornerRadius borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor{
    
    self.layer.cornerRadius = cornerRadius;
    self.layer.masksToBounds = YES;
    self.layer.borderWidth = borderWidth;
    self.layer.borderColor = borderColor.CGColor;
    
}
- (void)viewWithcornerRadius:(CGFloat)cornerRadius borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor backgroundColor:(UIColor *)backgroundColor{
    
    self.backgroundColor = backgroundColor;
    [self viewWithcornerRadius:cornerRadius borderWidth:borderWidth borderColor:borderColor];
    
}


- (void)addOpacityForever_Animation:(CGFloat)time
{
    [self.layer addAnimation:[self opacityForever_Animation:time] forKey:nil];
}

- (void)addRotation:(float)dur degree:(float)degree direction:(int)direction repeatCount:(int)repeatCount
{
    [self.layer addAnimation:[self rotation:dur degree:degree direction:direction repeatCount:repeatCount] forKey:nil];
}


#pragma mark === 永久闪烁的动画 ======
-(CABasicAnimation *)opacityForever_Animation:(float)time
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];//必须写opacity才行。
    animation.fromValue = [NSNumber numberWithFloat:1.0f];
    animation.toValue = [NSNumber numberWithFloat:0.0f];//这是透明度。
    animation.autoreverses = YES;
    animation.duration = time;
    animation.repeatCount = MAXFLOAT;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    animation.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];///没有的话是均匀的动画。
    return animation;
}

#pragma mark ====旋转动画======
-(CABasicAnimation *)rotation:(float)dur degree:(float)degree direction:(int)direction repeatCount:(int)repeatCount
{
    //  CATransform3D rotationTransform = CATransform3DMakeRotation(degree, 0, 0, direction);
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.fromValue = [NSNumber numberWithFloat:0.f];
    animation.toValue =  [NSNumber numberWithFloat: M_PI *2];
    animation.duration  =  dur;
    //    animation.autoreverses = NO;
    animation.cumulative = YES;
    animation.fillMode = kCAFillModeForwards;
    animation.repeatCount = MAXFLOAT;
    animation.removedOnCompletion = NO;
    
    return animation;
    
}
@end
